import LocalMap from "../../cfw/storage/LocalMap";
import SDKManager from "../../sdk/SDKManager";
import { FileEncoding, ResultState, DataCallback } from "../../sdk/SDKConfig";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { EventName } from "../../public/Config";

export default class TextureInfo {

    static WRITE_FILE_FINISH: string = 'write_file_finish'

    private save: LocalMap
    private _w: number = 0;

    private _h: number = 0

    private _data: ArrayBufferView;

    private _fomart;

    static createByTexture(texture: cc.RenderTexture) {
        let tex = new TextureInfo()
        tex.init(texture.width, texture.height, texture.readPixels(), texture.getPixelFormat())
        return tex;
    }

    writeFile(fileName: string) {
        if (!this.save) {
            this.save = new LocalMap(fileName, {})
        }
        SDKManager.getChannel().writeFile(fileName, this._data.buffer, FileEncoding.BINARY, (s: ResultState) => {
            this.save.set('width', this.width)
            this.save.set('height', this.height)
            this.save.set('fomart', this.fomart)
            this.save.set('fileName', fileName)

            GlobalEvent.instance().emit(TextureInfo.WRITE_FILE_FINISH)
        })
    }

    readFile2(fileName: string, callback: Function) {
        if (!this.save) {
            this.save = new LocalMap(fileName, {})
        }
        SDKManager.getChannel().readFile(fileName, FileEncoding.BINARY, (state, bytes) => {
            if (state == ResultState.YES) {
                let width = this.save.get('width')
                let height = this.save.get('height')
                let fomart = this.save.get('fomart')
                // console.log(' SDKManager.getChannel().readFile TextureInfo ', fileName)
                // try {
                let length = bytes.length
                if (length <= 0) {
                    return;
                }
                let count = width * height;
                this.init(width, height, bytes, fomart)
                callback(null,this)

            }else{
                callback('error ',this)
            }
        })
    }

    readFile(fileName: string, callback: Function) {
        if (!this.save) {
            this.save = new LocalMap(fileName, {})
        }
        SDKManager.getChannel().readFile(fileName, FileEncoding.BINARY, (state, bytes) => {
            if (state == ResultState.YES) {
                let width = this.save.get('width')
                let height = this.save.get('height')
                let fomart = this.save.get('fomart')
                // console.log(' SDKManager.getChannel().readFile TextureInfo ', fileName)
                // try {
                let length = bytes.length
                if (length <= 0) {
                    return;
                }
                let count = width * height;
                this.init(width, height, bytes, fomart)
                callback(this)

            }
        })
    }

    init(w: number, h: number, data: ArrayBufferView, fomart: any) {
        this._w = w;
        this._h = h;
        this._data = data;
        this._fomart = fomart;
    }

    get width() {
        return this._w;
    }

    get height() {
        return this._h;
    }

    get fomart() {
        return this._fomart;
    }

    get data() {
        return this._data;
    }
}
